import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# chatgpt
class ChatGpt:
    # 项目目录
    root_path = os.getcwd()
    # 浏览器驱动
    executable_path = "\chromedriver\\125\\chromedriver.exe"

    def __init__(self, root_path=False):
        # 驱动地址
        root_path = root_path if root_path else self.root_path
        service = ChromeService(executable_path=root_path + self.executable_path)

        # 访问参数设置
        chrome_options = Options()
        chrome_options.add_argument("--disable-infobars")
        chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
        chrome_options.add_experimental_option('useAutomationExtension', False)

        # 创建句柄
        self.browser = webdriver.Chrome(service=service, options=chrome_options)

    # 获取文本内容
    def send_text_message(self, question):
        self.browser.get("https://chatgpt.com/")

        # 点击页面
        wait = WebDriverWait(self.browser, 10)  # 10秒的超时时间
        tag = wait.until(EC.presence_of_element_located((By.ID, "prompt-textarea")))
        time.sleep(2)
        tag.send_keys("你好，你是？")
        time.sleep(2)
        tag.send_keys(Keys.ENTER)
        time.sleep(60*5)

        self.browser.close()